ocp: fix to free hwcomp log desc memory allocated#2690
ocp: fix to free hwcomp log desc memory allocated#2690igaw merged 1 commit intolinux-nvme:masterfrom
Conversation
| if (ret) | ||
| print_info_error("error: ocp: failed get hwcomp log: %02X data, ret: %d\n", | ||
| OCP_LID_HWCOMP, ret); | ||
| return ret; |
There was a problem hiding this comment.
if the command fails, get_hwcomp_log_data is in charge to free any allocated buffer. That means here the function should just return and after osp_show_hwcomp_log just always free the buffer. Note, free has a built in NULL check, so if (ptr) free(ptr) is not necessary anymore.
There was a problem hiding this comment.
The function nvme_get_log_page() called in get_hwcomp_log_data() but if failed the call then the allocated log->desc not freed so needed to free by the caller or the error return point.
static int get_hwcomp_log_data(struct nvme_dev *dev, struct hwcomp_log *log)
{
...
log->desc = calloc(1, args.len);
if (!log->desc) {
fprintf(stderr, "error: ocp: calloc: %s\n", strerror(errno));
return -1;
}
args.log = log->desc,
args.lpo = desc_offset,
#ifdef HWCOMP_DUMMY
memcpy(log->desc, &hwcomp_dummy[desc_offset], args.len);
#else /* HWCOMP_DUMMY */
ret = nvme_get_log_page(dev_fd(dev), NVME_LOG_PAGE_PDU_SIZE, &args);
if (ret) {
print_info_error("error: ocp: failed to get log page (hwcomp: %02X, ret: %d)\n",
OCP_LID_HWCOMP, ret);
return ret;
}
#endif /* HWCOMP_DUMMY */Note, free has a built in NULL check, so if (ptr) free(ptr) is not necessary anymore.
That is right. Thank you.
There was a problem hiding this comment.
A well behaving function should not change anything on error. So if a function allocates something and then fails later on the function is responsible to free the allocated resources. On the other hand if the function was successful and returns some resources to the caller, the caller is responsible for the resource now.
This makes the error handling on the caller side way more sane.
In the above code path, the correct thing is
ret = nvme_get_log_page(dev_fd(dev), NVME_LOG_PAGE_PDU_SIZE, &args);
if (ret) {
print_info_error("error: ocp: failed to get log page (hwcomp: %02X, ret: %d)\n",
OCP_LID_HWCOMP, ret);
free(log->desc);
return ret;
}There was a problem hiding this comment.
Understood well. Thank you for your advice and update the patch.
eb91108 to
4950ecd
Compare
|
Just only fixed to free without the NULL pointer checking at first. |
Since the desc pointer used _cleanup_free_ only set NULL value. Signed-off-by: Tokunori Ikegami <ikegami.t@gmail.com> [wagi: free buffer on error in get_hwcomp_log_data] Signed-off-by: Daniel Wagner <wagi@kernel.org>
|
I've updated |
|
Thanks! |
Since the desc pointer used cleanup_free only set NULL value.